/**
 * 
 */
package top.lcpsky.flashcards.web;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;

import top.lcpsky.flashcards.common.factory.PageFactory;
import top.lcpsky.flashcards.common.page.Page;
import top.lcpsky.flashcards.common.page.PageInfoBT;
import top.lcpsky.flashcards.entity.Site;
import top.lcpsky.flashcards.service.WebsiteService;
import top.lcpsky.flashcards.util.JsonUtils;
import top.lcpsky.flashcards.util.Pager;

/**
 * @ClassName: NavToolController
 * @author: lcp
 * @date: 2020年6月17日 下午2:17:35
 * 
 * @Copyright: 2020 www.ankept.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("site")
public class NavToolController {
	@Autowired
	private WebsiteService websiteService;
	@Autowired
	StringRedisTemplate redisTemplate;

	@RequestMapping(value = "/list")
	@ResponseBody
	public PageInfoBT list(@RequestParam(required = false) String title) {
		if (title == null) {
			title = "";
		}
		String content;
		if ((content = redisTemplate.opsForValue().get("site:list:" + title)) != null) {
			System.out.println(content);
			return JsonUtils.jsonToPojo(content, PageInfoBT.class);
		}
		Map map = new HashMap();
		map.put("title", title);
		Page<Site> page = new PageFactory<Site>().defaultPage();
		Pager<Site> pages = websiteService.getPage(map, page.getOffset(), page.getLimit());
		page.setTotal(pages.getTotal());
		page.setRecords(pages.getList());
		PageInfoBT resBt;
		content = JsonUtils.objectToJson(resBt = new PageInfoBT(page));
		redisTemplate.opsForValue().set("site:list:" + title, content, 3000, TimeUnit.SECONDS);
		return resBt;
	}
}
